<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use app\common\controller\Email;
use app\common\controller\Get;
use app\common\controller\Common;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;

/**
 * 采购订单
 * Class User
 * @package app\worker\controller
 */
class WhPurchase extends Controller
{

    protected $table = 'product_purchase';
    /**
     * 采购订单
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '采购订单';
        $this->ware_house_id = request()->get('ware_house_id');
        $query = $this->_query($this->table)
            ->dateBetween('create_at')
            ->where(['is_deleted' => '0'])
            ->where('ware_house_id', $this->ware_house_id)
            ->like('title')
            ->equal('status')
            ->order('create_at desc, id desc');
        $query->page();
    }

    protected function _index_page_filter(&$data)
    {
    }

    /**
     * 添加采购订单
     * @auth true
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 修改采购订单
     * @auth true
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     */
    protected function _form_filter(&$data)
    {
        if (request()->isPost()) {
            if(empty($data['product_ids'])){
                $this->error('请选择要采购的商品');
            }
            $sum_price = 0;
            foreach($data['content']['product_id'] as $k=>$v){
                $content[] = [
                    'product_id' => $data['content']['product_id'][$k],
                    'name' => $data['content']['name'][$k],
                    'sku' => $data['content']['sku'][$k],
                    'price' => $data['content']['price'][$k],
                    'count' => $data['content']['count'][$k],
                    'sum_price' => $data['content']['sum_price'][$k],
                ];
                $sum_price += $data['content']['sum_price'][$k];
            }
            $data['content'] = json_encode($content);
            $data['sum_price'] = $sum_price;
        }else{
            $data['content'] = !empty($data['content']) ? json_decode($data['content'], true) : [];
        }
    }

    /**
     * 确认入库
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $res = Db::name($this->table)->where('id', request()->post('id'))->find();
        
        $content = json_decode($res['content'], true);
        
        $data = [];
        foreach($content as $v){
            $result = Db::name('product_warehouse')->where('product_id', $v['product_id'])->where('ware_house_id', $res['ware_house_id'])->find();
            if($result){
                //存在修改库存
                Db::name('product_warehouse')->where('id', $result['id'])->setInc('count', $v['count']);
            }else{
                //不存在添加
                $data[] = [
                    'ware_house_id' => $res['ware_house_id'],
                    'product_id' => $v['product_id'],
                    'count' => $v['count'],
                ];
            }
        }
        Db::name('product_warehouse')->insertAll($data);
        Db::name($this->table)->where('id', request()->post('id'))->update(['status' => request()->post('status')]);
        $this->success('入库成功');
    }

    /**
     * 删除采购订单
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    public function get_products(){
        //成员信息列表
        $where = [
            ['is_deleted', '=', 0],
            ['status', '=', 1],
        ];
        $products = Db::name('product')->where($where)->order('create_at desc')->select();
        $this->success('获取成功！', $products);
    }

    public function content()
    {
        $this->content = json_decode(request()->get('content'), true);
        $this->fetch();
    }
}
